package ConcurrentProgramming;

// 发射任务模拟
@SuppressWarnings("all")

// 创建一个修改下列发射对象countDown的任务
class ChangeCountDown implements Runnable {
    private demoLifoOff demoLifoOff;

    public ChangeCountDown(demoLifoOff demoLifoOff) {
        this.demoLifoOff = demoLifoOff;
    }

    @Override
    public void run() {
        demoLifoOff.countDown = 5; // 将countDown修改为5
    }
}


public class demoLifoOff implements Runnable{
    protected int countDown = 10;
    private static int taskCount = 0;
    private final int id = taskCount+1;
    public demoLifoOff() {

    }
    public demoLifoOff(int countDown) {
        this.countDown = countDown;
    }
    public String status() {
        return "#" + id + "(" + ((countDown > 0) ? countDown : "LiftOff!") + ")";
    }

    public int getId() {
        return id;
    }


    // 定义发射任务
    @Override
    public void run() {
        while (countDown-- > 0) {
            System.out.println(status());
            try {
                Thread.sleep(1000);
                /**
                 * yield()方法的含义：线程让步
                 * 即将当前线程让出资源，从运行状态变为就绪状态，其他的线程来争夺这个被让出的资源
                 * 并不一定是比当前线程优先级高的才能获取，低的同样有概率获取，但是概率比较低
                 */
                if (countDown == 5) {
                    int i = 1 / 0; // 手动异常
                }
                Thread.yield();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {
        /*demoLifoOff demoLifoOff = new demoLifoOff(10);
        ChangeCountDown changeCountDown = new ChangeCountDown(demoLifoOff);
        demoLifoOff.run();
        changeCountDown.run();*/
    }
}
